/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package traffic_lights;

import java.util.Scanner;

/**
 * This class controls the Menu and is able to deal with user input accordingly and pass it on to the Intersection class file.
 * The menu also contains some user proofing.
 * @author Luke Bemringham
 */
public class Main {
    /** The root of program, the menu. It has multiple menu options and is able to pass on the user selection to the intersection class file.
     * The menu options also contain user proofing for bad inputs and loops accordingly until the simulation is run.
     * The user input in the menu is implemented through Scanners.
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Intersection Intersection = new Intersection(); //instatiates intersection class
        while (true){
        System.out.println("=== MENU ===\n"
                + " 1. Set Number of horizontal lanes (H-street) [min 1, max 3]\n"
                + " 2. Set Number of vertical lanes (V-street) [min 1, max 4] \n"
                + " 3. Set Probability of a car entering H-street [min 0, max 1] \n"
                + " 4. Set Probability of a car entering V-street [min 0, max 1] \n"
                + " 5. Run one simulation cycle \n"
                + " 6. Set and run number of simulation cycles [min 1, max 10] \n"
                + " Enter your choice>"); //displays the menu


        Scanner input = new Scanner(System.in); //open scanner for keyboard input
        String menuChoice = input.nextLine(); //make string of user input

        if (menuChoice.equals("1")) {
            System.out.println("1. Set Number of horizontal lanes (H-street) [min 1, max 3]");
            String h_Lanes = input.nextLine(); //input for number of horizontal lanes

            if (h_Lanes.equals("1") || h_Lanes.equals("2") || h_Lanes.equals("3")) {
                Intersection.setH_Lanes(h_Lanes); //pass user generated lanes to intersection
            } else {
                System.out.println("Invalid Input. 1-3 required.");
            }
        } else if (menuChoice.equals("2")) {
            System.out.println("2. Set Number of vertical lanes (V-street) [min 1, max 4]");
            String v_Lanes = input.nextLine();

            if (v_Lanes.equals("1") || v_Lanes.equals("2") || v_Lanes.equals("3") || v_Lanes.equals("4")){
                Intersection.setV_Lanes(v_Lanes); //pass user generated lanes to intersection
            } else {
                System.out.println("Invalid Input. 1-4 required.");
            }
        } else if (menuChoice.equals("3")) {
            System.out.println("3. Set Probability of a car entering H-street [min 0, max 1]");
            String h_Car = input.nextLine();
            try {
                float h_carFlt = Float.valueOf(h_Car.trim()).floatValue(); //make h_Car into an float
                if ((h_carFlt > 0 || h_carFlt == 0) && (h_carFlt < 1 || h_carFlt == 1)) { //is number equal to or between 0 and 1
                    Intersection.setHProb(h_carFlt); //use setter on user input probabilty
                } else {
                    System.out.println("Invalid entry. Between 0 and 1 accepted.");
                }
            } catch (NumberFormatException nfe) {
                System.out.println("Invalid entry. Between 0 and 1 accepted."); //if string to float conversion failed throw exeception
            }
        } else if (menuChoice.equals("4")) {
            System.out.println("4. Set Probability of a car entering V-street [min 0, max 1]");
            String v_Car = input.nextLine();
            try {
                float v_carFlt = Float.valueOf(v_Car.trim()).floatValue(); //make v_Car into an float
                if ((v_carFlt > 0 || v_carFlt == 0) && (v_carFlt < 1 || v_carFlt == 1)) { //is number equal to or between 0 and 1
                    Intersection.setVProb(v_carFlt); //pass probability of a vertical car to intersection
                } else {
                    System.out.println("Invalid entry. Between 0 and 1 accepted.");
                }
            } catch (NumberFormatException nfe) {
                System.out.println("Invalid entry. Between 0 and 1 accepted."); //if string to float conversion failed throw exeception
            }
        } else if (menuChoice.equals("5")) {
            Intersection.initiate("1"); //enter "1" into simulation for one run through
            break; //ends menu loop
        } else if (menuChoice.equals("6")) {
            System.out.println("6. Set and run number of simulation cycles [min 1, max 10]");
            String numSims = input.nextLine();
            try {
                int numSims_Int = Integer.parseInt(numSims); //make numSims into an integer
                if ((numSims_Int > 0 || numSims_Int == 0) && (numSims_Int < 10 || numSims_Int == 10)) { //is number equal to or between 0 and 10
                    Intersection.initiate(numSims);
                    break; //ends menu loop
                } else {
                    System.out.println("Invalid entry. Between 0 and 10 accepted.");
                }
            } catch (NumberFormatException nfe) {
                System.out.println("Invalid entry. Between 0 and 10 accepted."); //if string to float conversion failed throw exeception
            }
        } else {
            System.out.println("Bad Menu Choice."); // if the menu selection is not 1-6
        }

    }}
}
